package cn.com.zetatech.loader.entity;

import javax.xml.soap.SAAJResult;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RowToColumnTransform {
    // Define the 2D array
    private static final Object[][] dataArray = {
            {2.8, "v_2point8"},
            {2.9, "v_2point9"},
            {2.6, "v_2point6"},
            {2.5, "v_2point5"}
    };

    public static void main(String[] args) {
        // Example values for wafer_id and die_X
        String waferId = "12345";
        String dieX = "6789";
        String v_2point9 = String.valueOf(3);
        String v_2point8 = String.valueOf(2);

        Map<String, Object> map = new HashMap<>();
        map.put("wafer_id", waferId);
        map.put("die_X", dieX);
        if (v_2point9 != null) map.put("v_2point9", v_2point9);
        if (v_2point8 != null) map.put("v_2point8", v_2point8);
        if (v_2point8 != null) map.put("v_2point8", v_2point8);
        // Skip v_2point6 and v_2point5 if no values are provided

        // Transform the row data and print the results
        List<Map<String, Object>> outputMaps = transformRowToColumns(map);

        // Print the transformed rows
        for (Map<String, Object> row : outputMaps) {
            System.out.println(row);
        }
    }

    public static List<Map<String, Object>> transformRowToColumns(Map<String, Object> map) {


        List<Map<String, Object>> transformedRows = new ArrayList<>();

        // Iterate over each entry in the dataArray
        for (Object[] group : dataArray) {
            double value = (double) group[0];
            String label = (String) group[1];

            // Retrieve the voltage value from the map
            Integer voltage = (Integer) map.get(label);
            if (voltage != null) {
                // Create a new Map with wafer_id, die_X, value, and voltage
                Map<String, Object> transformedRow = new HashMap<>();
                transformedRow.put("wafer_id",transformWafersId(map.get("")) );
                transformedRow.put("die_X", transformDieX(map.get("")));
                transformedRow.put("value", value);
                transformedRow.put("voltage", voltage);

                // Add the transformed row to the list
                transformedRows.add(transformedRow);
            }
        }

        return transformedRows;
    }

    private static String transformWafersId(Object waferId) {
        return waferId.toString().replace("/", "0");
    }

    private static String transformDieX(Object dieX) {
        return dieX.toString() + "op";
    }
}
